package cn.edu.pzhu.javaweb.controller;

import cn.edu.pzhu.javaweb.pojo.FileMsg;
import cn.edu.pzhu.javaweb.util.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/showfile")
public class ShowAllFileServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public ShowAllFileServlet() {}

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<FileMsg> fileList = new ArrayList<>();

        try {
            // 1. 加载数据库驱动
            //Class.forName(DRIVER_CLASS);

            // 2. 获取数据库连接
            //conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            conn = DBUtils.getConnection();

            // 3. 编写SQL查询语句
            String sql = "SELECT fileid, username, filename, classification, filepath, releasedate, rating, description FROM filemsg";

            // 4. 创建PreparedStatement
            pstmt = conn.prepareStatement(sql);

            // 5. 执行查询，获取结果集
            rs = pstmt.executeQuery();

            // 6. 处理结果集，映射为List<FileMsg>
            while (rs.next()) {
                FileMsg fileMsg = new FileMsg();
                fileMsg.setFileId(rs.getInt("fileid"));
                fileMsg.setUserName(rs.getString("username"));
                fileMsg.setFileName(rs.getString("filename"));
                fileMsg.setClassification(rs.getString("classification"));
                fileMsg.setFilePath(rs.getString("filepath"));
                fileMsg.setReleasedate(rs.getDate("releasedate"));  // 注意：java.sql.Date是java.util.Date的子类
                fileMsg.setRating(rs.getDouble("rating"));
                fileMsg.setDescription(rs.getString("description"));

                fileList.add(fileMsg);
            }

        }catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException("数据库操作异常", e);
        } finally {
            // 7. 关闭资源（注意关闭顺序：ResultSet -> PreparedStatement -> Connection）
            DBUtils.closeResources(rs, pstmt, conn);

            // 8. 将结果存入Session域
            HttpSession session = request.getSession();
            session.setAttribute("list", fileList);

            // 9. 可以重定向或转发到页面（根据实际需求修改）
            // 示例：转发到文件列表页面
            request.getRequestDispatcher("/fileList.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
